package com.mark.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mark.dao.UserDao;
import com.mark.domain.User;


public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserDao dao = new UserDao();



	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		User user = dao.getUser(username, password);
		if(user == null) {
			request.setAttribute("message", "login fail");
		} else if(user.getUsername().equals(username) && user.getPassword().equals(password)) {
			request.setAttribute("message", "welcome "+username);
		} 
		/*
		if (username != null && password != null && username.equals("mark") && password.equals("1234")) {
			request.setAttribute("message", "welcome "+username);
		} else {
			request.setAttribute("message", "login fail");
		}*/
		request.getRequestDispatcher("message.jsp").forward(request, response);
	}

}
